package com.lrm.dao;

import com.lrm.pojo.Blog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2017/12/1.
 * @Modified By:
 */
public interface BlogJPA extends JpaRepository<Blog,Long>,JpaSpecificationExecutor<Blog>
{
    @Query("select b from Blog b where b.recommend=true ")
    List<Blog> blogrecommendList(Pageable pageable);

    @Query("select b from Blog b where b.title like ?1 or b.content like ?1")
    Page<Blog> blogSenrchList(String senrch,Pageable pageable);


    @Modifying//必须要在事物中执行
    @Query("update Blog b set b.views=b.views+1 where b.id = ?1")
    int updataViews(Long id);

    @Query("select function('date_format',b.updataTime,'%Y') as year from Blog b group by function('date_format',b.updataTime,'%Y') order by b.updataTime asc ")
    List<String> findGroupYear();

    @Query("select b from Blog b where function('date_format',b.updataTime,'%Y') = ?1 order by b.updataTime desc")
    List<Blog> findByYear(String year);
}
